<?php
/*
 * Created on May 4, 2012
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
require_once("dao/IDao.php");
require_once("model/ImportStatus.php");

class ImportStatusDAO implements IDao{
	var $conn;
	
	public function __construct($conn){
		$this->conn =& $conn;
	}
	
	public function getItem($id){}
	
	public function createImportStatus($imptStatus){
		$importStatusID = null;
		
		try{
			if($stmt = $this->conn->prepare("CALL IMPORT_STATUS_CREATE(?,?,?,?)")){
				$stmt->bind_param("isii",$imptStatus->adminId, $imptStatus->fileName,$imptStatus->unvPlayerCount,$imptStatus->unvCourseCount);
				$stmt->execute();
				
				$stmt->bind_result($_importStatusID);
				while($stmt->fetch()){
					$importStatusID = $_importStatusID;
				}
				if($stmt->error != "")
					throw new Exception($stmt->error);
				$stmt->close();
			}
			else{
				echo "Failed to prepare the statement. ".$stmt->error;
			}
				
		}
		catch(Exception $e){
			throw new Exception($e->getMessage());
		}
		
		$this->conn->close();
		return $importStatusID;		
	}
	
	public function getImportStatus($adminId,$imptStatusId){
		$result = null;
		try{
			if($stmt = $this->conn->prepare("CALL IMPORT_STATUS_READ_LIST(?,?)")){
				$result = array();
				$stmt->bind_param("ii",$adminId,$imptStatusId);
				$stmt->execute();
				
				$stmt->bind_result($_importStatusId,$_fileName,$_unverifiedPlayerCount,$_unverifiedCourseCount,$_adminId,$_dataLoadedData,$lastUpdate);
				
				while($stmt->fetch()){
					$is = new ImportStatus();
					$is->importStatusId = $_importStatusId;
					$is->fileName = $_fileName;
					$is->unverifiedPlayerCount = $_unverifiedPlayerCount;
					$is->unverifiedCourseCount = $_unverifiedCourseCount;
					$is->adminId = $_adminId;
					$is->dataLoadedDate = $_dataLoadedData;
					$is->lastUpdate = $lastUpdate;
					
					array_push($result,$is);
				}
				
				$stmt->close();
			}
			else{
				throw new Exception($stmt->error);
			}	
		}
		catch(Exception $e){
			throw new Exception($e->getMessage());
		}
		
		$this->conn->close();
		
		return $result;
	}
	
	public function saveItem($importStatus){
		
	}
	
	public function deleteItem($id){
		try{
			if($stmt = $this->conn->prepare("CALL IMPORT_STATUS_DELETE(?)")){
				$stmt->bind_param("i",$id);
				$stmt->execute();
				$stmtError = $stmt->error;
				$stmt->close();
				if($stmtError != ""){
					throw new Exception($stmtError);
				}
			}
			else{
				throw new Exception("Failed to prepare the statement in \"deleteImportStatus\". ".$stmt->error);
			}	
		}
		catch(Exception $e){
			throw new Exception($e->getMessage());
		}
		$this->conn->close();
	}
	
	public function __destruct(){}
}
?>
